﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PInitiatives.Models;
using PInitiatives.ViewModels.VMReviews;
using PInitiatives.Helpers;

namespace PInitiatives.Controllers
{
    public class ReviewController : Controller
    {
        PInitiativesEntities _db = new PInitiativesEntities();

        [Authorize]
        public ActionResult ToReview(bool ToReview = false)
        {
            VMToReview itr = new VMToReview();
            int currentUserId = UserHelper.GetCurrentUserId();
            User currentUser = UserHelper.GetCurrentUser();

            //Participated initatives to review
            var participatedInitativesToReview = from iu in _db.InitiativeUsers
                                                 from i in _db.Initiatives
                                                 where iu.UserID == currentUserId
                                                 where iu.State == "Participated"
                                                 where i.ID == iu.InitiativeID
                                                 where i.State == "Finished"
                                                 select i;

            //var initativesToReview = UserHelper.GetCurrentUser().getParticipatedInitiatives("Participated").Where(i => i.State == "Finished");
            if (_db.Reviews.FirstOrDefault() != null)
            {
                var initatives = from i in participatedInitativesToReview
                                 where !_db.Reviews.Any(r => r.InitiativeID == i.ID && r.RatingUserID == currentUserId)
                                 select i;

                itr.ParticipatedInitatives = initatives;
            }
            else
                itr.ParticipatedInitatives = participatedInitativesToReview;


            //Created initatives to review
            itr.CreatedInitatives = from i in currentUser.CreatedInitiatives
                                    where i.State == "Finished"
                                    where !_db.Reviews.Any(r => r.InitiativeID == i.ID && r.RatingUserID == currentUserId)
                                    select i;

            return View(itr);

        }
        
        [Authorize]
        public ActionResult InitiativeReview(int id)
        {
            VMInitativeReview reviewVM = new VMInitativeReview();
            int currentUserId = UserHelper.GetCurrentUserId();
            var initativeReview = _db.Reviews.SingleOrDefault(r => r.RatingUserID == currentUserId && r.InitiativeID == id);
            if (initativeReview != null)
            {
                ViewBag.HasRate = true;
                reviewVM = new VMInitativeReview(initativeReview.AuthorRate, initativeReview.InitiativeRate, initativeReview.Description);
            }
            else
            {
                ViewBag.HasRate = false;
                reviewVM.AuthorRate = 5;
                reviewVM.InitiativeRate = 5;
            }
            reviewVM.Initative = _db.Initiatives.Find(id);
            return View(reviewVM);
        }

        [Authorize]
        [HttpPost]
        public ActionResult InitiativeReview(VMInitativeReview rvm, int id)
        {
            Initiative initiative = _db.Initiatives.Find(id);
            User initativeAuthor = initiative.InitiativeAuthor;
            int ratingUserId = UserHelper.GetCurrentUserId();//_db.Users.SingleOrDefault(u=>u.aspnet_Users.UserName == HttpContext.User.Identity.Name).ID;
            Review review = new Review(ratingUserId, initativeAuthor.ID, initiative.ID, rvm.AuthorRate, rvm.InitiativeRate, rvm.Description);
            _db.Reviews.Add(review);
            _db.SaveChanges();
            
            //refresh rates for initiative and user
            ReviewHelper.RefreshInitiativeRate(initiative.ID);
            ReviewHelper.RefreshUserRate(initativeAuthor.ID);

            return RedirectToAction("ToReview");
        }

        [Authorize]
        public ActionResult UserReview(int id)
        {
            Initiative ini = _db.Initiatives.SingleOrDefault(i => i.ID == id);
            List<VMUserRate> usersRate = new List<VMUserRate>();
            
            foreach (User user in ini.getInitiativeUsers())
                usersRate.Add(new VMUserRate(user.ID,5));

            //VMUserReview reviewVM = new VMUserReview(usersRate, ini);
            VMUserReview ur = new VMUserReview();
            ur.usersRate = usersRate;
            return View(ur);
        }

        [Authorize]
        [HttpPost]
        public ActionResult UserReview(VMUserReview usersReview, int id)
        {
            int ratingUserId = UserHelper.GetCurrentUserId();
            foreach (VMUserRate userRate in usersReview.usersRate)
            {
                Review review = new Review(ratingUserId, userRate.UserId, id, userRate.Rate, -1, userRate.Description);
                _db.Reviews.Add(review);
                _db.SaveChanges();

                ReviewHelper.RefreshUserRate(userRate.UserId);
            }
            //_db.SaveChanges();
            return View("ToReview", new { id = ratingUserId});
        }

    }
}
